package com.lv.phuotbacnam.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.data.BinhChonObject;
import com.lv.phuotbacnam.bl.log.Toaster;
import com.lv.phuotbacnam.bl.queries.BaiDangQueries;
import com.lv.phuotbacnam.bl.queries.BinhChonQueries;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.utils.ImageLoaderCus;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.lv.phuotbacnam.toolbox.JsonUtils;
import com.lv.phuotbacnam.ui.Constants;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseUser;

public class ChiTietDiaDiemSupport {
	private Context mContext;
	private LinearLayout bai_dang_list;
	private LinearLayout nhan_xet_list;
	private ParseObject dia_diem;
	private CallBackFromParse<Object, Object> callBack;

	LayoutInflater inflater;

	/* use to display image from url */
	public ImageLoaderCus imageLoader;

	public ChiTietDiaDiemSupport(Context mContext, LinearLayout bai_dang_list,
			LinearLayout nhan_xet_list, ParseObject dia_diem,
			CallBackFromParse<Object, Object> call_back) {
		super();
		this.mContext = mContext;
		this.bai_dang_list = bai_dang_list;
		this.nhan_xet_list = nhan_xet_list;
		this.dia_diem = dia_diem;
		this.callBack = call_back;

		inflater = (LayoutInflater) this.mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		initImageLoader();
	}

	public Context getmContext() {
		return mContext;
	}

	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}

	public LinearLayout getBai_dang_list() {
		return bai_dang_list;
	}

	public void setBai_dang_list(LinearLayout bai_dang_list) {
		this.bai_dang_list = bai_dang_list;
	}

	public LinearLayout getNhan_xet_list() {
		return nhan_xet_list;
	}

	public void setNhan_xet_list(LinearLayout nhan_xet_list) {
		this.nhan_xet_list = nhan_xet_list;
	}

	public ParseObject getDia_diem() {
		return dia_diem;
	}

	public void setDia_diem(ParseObject dia_diem) {
		this.dia_diem = dia_diem;
	}

	/**
	 * To init image loader
	 */
	private void initImageLoader() {
		imageLoader = new ImageLoaderCus(mContext);
	}

	// -------------------------------------------------------------
	// Method
	/**
	 * To show list of bai dang
	 */
	public void showListBaiDang() {
		bai_dang_list.addView(progressBarLoading());
		BaiDangQueries.getBaiDangByDiaDiem(dia_diem,
				new CallBackFromParse<Object, List<ParseObject>>() {
					@Override
					public void done(Object request, List<ParseObject> baidangs) {
						bai_dang_list.removeAllViews();
						for (ParseObject baidang : baidangs) {
							View childLayout = inflater.inflate(
									R.layout.bai_dang_list_item, null);
							showItemInBaiDang(childLayout, baidang);
							bai_dang_list.addView(childLayout);
							bai_dang_list.addView(dividerView());
						}
					}
				});
	}

	/**
	 * To show info into layout
	 * 
	 * @param layout
	 * @param baidang
	 */
	private void showItemInBaiDang(View layout, final ParseObject baidang) {
		TextView ten_bai_dang = (TextView) layout
				.findViewById(R.id.ten_bai_dang);
		ImageView img = (ImageView) layout.findViewById(R.id.img);
		TextView noi_dung = (TextView) layout.findViewById(R.id.noi_dung);
		TextView so_luot_thich = (TextView) layout
				.findViewById(R.id.so_luot_thich);
		TextView so_luot_nhan_xet = (TextView) layout
				.findViewById(R.id.so_luot_nhan_xet);

		ten_bai_dang.setText(baidang
				.getString(QueriesConstant.TableBaiDang.COLUMN_ten_bai_dang));
		so_luot_nhan_xet
				.setText(""
						+ baidang
								.getInt(QueriesConstant.TableBaiDang.COLUMN_luot_nhan_xet));
		so_luot_thich
				.setText(""
						+ baidang
								.getInt(QueriesConstant.TableBaiDang.COLUMN_luot_thich));
		noi_dung.setText(baidang
				.getString(QueriesConstant.TableBaiDang.COLUMN_cam_nhan));

		// show image
		ArrayList<String> list_images = JsonUtils.getStringList(baidang
				.getJSONArray(QueriesConstant.TableBaiDang.COLUMN_list_photo),
				"url");
		if (list_images.size() > 0)
			imageLoader.showImage(img, list_images.get(0));

		// OnclickLisener for baidang
		layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				callBack.done("", "");
				IntentHelper.addObjectForKey(baidang, Constants.KEY_BAI_DANG);
			}
		});
	}

	/**
	 * To show list of nhan xet
	 */
	public void showListNhanXet() {
		final ProgressBar loading = progressBarLoading();
		nhan_xet_list.addView(loading);
		BinhChonQueries.getBinhChonOfCurrentUser(dia_diem,
				new CallBackFromParse<Object, ParseObject>() {
					@Override
					public void done(Object request, ParseObject bc) {
						nhan_xet_list.removeView(loading);
						if (bc == null)
							nhan_xet_list.addView(vietBinhLuan());
						else
							showNhanXetCuaBan(bc);
					}
				});

		// TODO neu da binh chon thi show binh chon , neu khong thi show o de
		// nhap binh chon

		BinhChonQueries.getBinhChon(dia_diem,
				new CallBackFromParse<Object, List<ParseObject>>() {
					@Override
					public void done(Object request, List<ParseObject> binhchons) {
						if (binhchons != null && binhchons.size() > 0) {
							for (ParseObject binhchon : binhchons) {
								View childLayout = inflater.inflate(
										R.layout.nhan_xet_list_item, null);
								showItemNhanXet(childLayout, binhchon);
								nhan_xet_list.addView(dividerSmallView());
								nhan_xet_list.addView(childLayout);
							}
						}
					}
				});

	}

	private void showListNhanXetItem(View layout, final ParseObject nhanxet) {

	}

	/**
	 * To create a divider view programmatically
	 * 
	 * @return
	 */
	private View dividerView() {
		View v = new View(mContext);

		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
				(int) mContext.getResources().getDimension(
						R.dimen.view_big_height));
		v.setLayoutParams(layoutParams);
		v.setBackgroundColor(mContext.getResources().getColor(R.color.main_bg));

		return v;
	}

	/**
	 * To create a divider view programmatically
	 * 
	 * @return
	 */
	private View dividerSmallView() {
		View v = new View(mContext);

		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
				(int) mContext.getResources().getDimension(
						R.dimen.view_small_height));
		v.setLayoutParams(layoutParams);
		v.setBackgroundColor(mContext.getResources().getColor(R.color.main_bg));

		return v;
	}
	
	/**
	 * To create a ProgressBar programmatically
	 * 
	 * @return
	 */
	private ProgressBar progressBarLoading() {
		return new ProgressBar(mContext, null, android.R.attr.progressBarStyle);
	}

	private View vietBinhLuan() {
		final View viet_binh_luan = inflater.inflate(R.layout.viet_binh_luan,
				null);

		final EditText nhap_binh_luan = (EditText) viet_binh_luan
				.findViewById(R.id.nhap_binh_luan);
		final TextView seekBarValue = (TextView) viet_binh_luan
				.findViewById(R.id.seekBarValue);
		SeekBar seekBar = (SeekBar) viet_binh_luan
				.findViewById(R.id.seekBarDistance);

		seekBar.setProgress(5);
		seekBar.incrementProgressBy(1);
		seekBar.setMax(10);
		seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				progress = progress / 1;
				progress = progress * 1;
				// diem_binh_chon = progress;
				seekBarValue.setText(String.valueOf(progress));
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});

		viet_binh_luan.findViewById(R.id.send).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						String binh_luan_value = nhap_binh_luan.getText()
								.toString().trim();
						String seekBar_value = seekBarValue.getText()
								.toString().trim();

						if (seekBar_value.equals("")) {
							Toaster.showToast(mContext, mContext
									.getString(R.string.ban_phai_binh_chon));
						} else if (binh_luan_value.equals("")) {
							Toaster.showToast(
									mContext,
									mContext.getString(R.string.ban_phai_nhap_binh_luan));
						} else {
							// TODO save data
							final ProgressBar loading = progressBarLoading();
							nhan_xet_list.removeView(viet_binh_luan);
							nhan_xet_list.addView(loading, 0);

							BinhChonObject binhChonObject = new BinhChonObject(
									Integer.parseInt(seekBar_value),
									binh_luan_value);
							BinhChonQueries
									.createNewBinhChon(
											binhChonObject,
											dia_diem,
											new CallBackFromParse<Object, ParseObject>() {
												@Override
												public void done(
														Object request,
														ParseObject binhchon) {
													nhan_xet_list
															.removeView(loading);
													showNhanXetCuaBan(binhchon);
												}
											});
						}
					}
				});

		return viet_binh_luan;
	}

	/**
	 * To show comment of current user
	 * 
	 * @param layout
	 * @param binhChonObject
	 */
	private void showNhanXetCuaBan(ParseObject binhchon) {
		View childLayout = inflater.inflate(R.layout.nhan_xet_list_item, null);
		childLayout.findViewById(R.id.img_bacham).setVisibility(View.VISIBLE);
		showItemNhanXet(childLayout, binhchon);
		nhan_xet_list.addView(childLayout, 0);
	}

	/**
	 * To show item nhanxet
	 */
	private void showItemNhanXet(View layout, ParseObject binhchon) {
		final TextView user_name = (TextView) layout
				.findViewById(R.id.user_name);
		TextView rated = (TextView) layout.findViewById(R.id.rated);
		TextView noi_dung = (TextView) layout.findViewById(R.id.noi_dung);
		final ImageView avatar = (ImageView) layout.findViewById(R.id.avatar);
		ImageView img_bacham = (ImageView) layout.findViewById(R.id.img_bacham);

		rated.setText("Rated: "
				+ binhchon
						.getInt(QueriesConstant.TableBinhChon.COLUMN_diem_binh_chon));
		noi_dung.setText(binhchon
				.getString(QueriesConstant.TableBinhChon.COLUMN_nhan_xet));

		img_bacham.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO show dialog updating, deleting
				Toaster.showToast(mContext, "Will be show dialog");
			}
		});

		// Show user
		ParseUser user = binhchon
				.getParseUser(QueriesConstant.TableBinhChon.COLUMN_nguoi_binh_chon);
		user.fetchIfNeededInBackground(new GetCallback<ParseUser>() {
			@Override
			public void done(ParseUser parseUser, ParseException e) {
				if (e == null) {
					user_name.setText(parseUser
							.getString(QueriesConstant.TableUser.COLUMN_firstName)
							+ " "
							+ parseUser
									.getString(QueriesConstant.TableUser.COLUMN_lastName));
					imageLoader.showImage(
							avatar,
							parseUser
									.getString(QueriesConstant.TableUser.COLUMN_photoURL));
					avatar.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							// TODO goto details of user
							Toaster.showToast(mContext, "User's details");
						}
					});
				}
			}
		});

	}

}
